<?php
namespace plugins\member\controller;
use cmf\controller\PluginAdminBaseController;
use think\Db;
use app\common\lib\model_operate\Transit;
use plugins\member\validate\MemberValidate;
use plugins\member\model\MemberModel;
use plugins\member\interfaceLib\MemberClassify;
use plugins\finance\model\FinanceModel;
use plugins\order\model\OrderModel;
use plugins\marketing\model\MarketingIntegralRuleModel;

class AdminIndexController extends PluginAdminBaseController
{
	protected function _initialize()
	{

	}

	/**
	*	会员首页
	*/
	public function index()
	{
		$param = $this->request->param();

		//引用对应的tag文件
		$search = Transit::createTags('search');
		$search->setParam($param);
		$table = Transit::createTags('table');
		$table->setParam($search);
		$list = $table
				->query()
				->where("status != 3")	//3为已删除
				->select();	
		$this->assign('search',$search);
		$this->assign('table',$table);
		$this->assign('searchStr',$search->createHtml());
		$this->assign('list',$list);
		return $this->fetch();
	}

	/**
	*  意见反馈
	*/
	public function feedback()
	{
        $data = $this->request->param();
        $where = 1;
        $sear['pagesize'] = isset( $data['pagesize'] ) ? $data [ 'pagesize' ] : 20;
        $list = Db::name('feedback')->where($where)->paginates($sear['pagesize'] , false ,['query'=>$sear])->each(function ($item){
            $item['addtime'] = date('Y-m-d H:i:s',$item['addtime']);
            $member = Db::name('member')->where('uid',$item['userid'])->find();
            $nick_name = $item['userid'];
            if(!empty($member)){
                $nick_name = $member['nick_name'];
            }
            $item['nick_name'] = $nick_name;
            return $item;
        });
        $list->appends($data);
        $this->assign('list', $list);
        $this->assign('sear',$sear);
        $this->assign('page', $list->render());
		return $this->fetch();
	}



	//添加界面
	public function addPage()
	{
		//获取传值
		$data = $this->request->param();

		//新建中转方法
		$add = Transit::createTags('add');
		if($this->request->isPost()){
			//密码加密
			$data['password'] = cmf_password($data['password']);
			//注册时间
			$data['join_date'] = time();
			//注册ip
			$data['join_ip'] = $this->request->ip();
			$res = $add->saveAdd($data);
			if($res['code'] == 200){
				//uid的补充
			    $uid=str_pad($res['data'],4,"0",STR_PAD_LEFT);   
				$uid = 'zy_'.$uid.mt_rand(1000,9999);
				db('member')->where(['id'=>$res['data']])->update(['uid'=>$uid]);
				return $this->success($res['message']);
			}else{
				return $this->error($res['message']);
			}
		}
		$this->assign('addStr',$add->createHtml());
		return $this->fetch();
	}

	//修改界面
	public function editPage()
	{
		$data = $this->request->param();
		$edit = Transit::createTags('edit');
		//修改功能
		if($this->request->isPost()){
			$data = $this->request->post();
			$result = ( new MemberModel )->where(['id'=>$data['id'],'status'=>['neq',3]])->update($data);
			if($result){
				return $this->success('更新成功');
			}else{
				return $this->error('更新失败');
			}
		}

		$list = $edit->query()->where('id',$data['id'])->find();

		$this->assign('editStr',$edit->createHtml($list));

		return $this->fetch();

	}



	//详情界面
	public function detailPage()
	{
		$data = $this->request->param();

		//创建tag对象
		$detail = Transit::createTags('detail');

		//参数查询
		$list = $detail->query()->where('id',$data['id'])->find();

		//表格数据生成createTableHtml(参数,每行显示数据数|默认为2)
		$this->assign('detailStr',$detail->createTableHtml($list));
		$this->assign('list',$list);
		$this->assign('zy_domain',zy_domain());

		//member
		$member = ( new MemberModel )->get(['id'=>$data['id']]);

		//用户账户资产
		$finance = ( new FinanceModel )->get(['finance_type'=>'member','finance_type_id'=>$member->uid]);

		//账户余额
		$balance = $finance->balance;
		$this->assign('balance',$balance);

		//消费金额，订单总金额
		$order_sum = ( new OrderModel)
		->where(['order_status'=>['in',[2,3,4,10]],'member_id'=>$member->uid])
		->sum('order_money');
		$this->assign('order_sum',$order_sum);

		//消费次数，订单总数
		$order_count = ( new OrderModel)
		->where(['order_status'=>['in',[2,3,4,10]],'member_id'=>$member->uid])
		->count();	
		$this->assign('order_count',$order_count);

		//订单均价
        $order_average = 0;
        if($order_count>0){
            $order_average = $order_sum / $order_count;
        }

		$this->assign('order_average',$order_average);

		//营销
//		$market = ( new MarketingIntegralRuleModel)->bonusPoints($member->uid);
//		$this->assign('market',$market);
		//当前积分
		//累计积分
		//未使用优惠券
		//已使用优惠券
		//已过期优惠券

		return $this->fetch();
	}


	//删除用户
	public function delete()
	{
		$data = $this->request->post();
        if (empty($data['id'])) {
            return $this->error('请先选择要删除的用户！');
        }

        if (is_array($data['id'])) {
        	$result = ( new MemberModel )->where('id', 'in', $data['id'])->update(['status'=>3,'phone'=>'']);
        } else {
			$result = ( new MemberModel )->where(['id'=>$data['id']])->update(['status'=>3,'phone'=>'']);
        }

		if($result){
			return $this->success('删除成功');
		}else{
			return $this->error('删除失败');
		}        

	}



	//禁用用户
	public function forbidden(){
		$data = $this->request->post();
        if (empty($data['id'])) {
            return $this->error('请先选择要禁用的用户！');
        }
        
        if (is_array($data['id'])) {
        	$result = ( new MemberModel )->where('id', 'in', $data['id'])->update(['status'=>2]);
        } else {
			$result = ( new MemberModel )->where(['id'=>$data['id']])->update(['status'=>2]);
        }

		if($result){
			return $this->success('禁用成功');
		}else{
			return $this->error('禁用失败');
		}        
	}



	//启用用户
	public function startusing()
	{
		$data = $this->request->post();
        if (empty($data['id'])) {
            return $this->error('请先选择要启用的用户！');
        }

        if (is_array($data['id'])) {
        	$result = ( new MemberModel )->where('id', 'in', $data['id'])->update(['status'=>1]);
        } else {
			$result = ( new MemberModel )->where(['id'=>$data['id']])->update(['status'=>1]);
        }

		if($result){
			return $this->success('启用成功');
		}else{
			return $this->error('启用失败');
		} 		
	}

	public function pzUpload(){
		$param = $this->request->param();

		//模块的标识
		$module = $param['_plugin'];

		//文件夹的标识
		$info = $param['info'];

		//获取上传的文件
        $file = $_FILES['file'];	

        $res = fileUpload($file,$module,$info);

        if($res['status'] == true){
        	return json(['save'=>$res['data'],'info'=>$info]);
        }
	}	



}



?>